var sysId = "";
var nodeId = "";
var nodeName = "";
var tableName = "";
var nodeTypeNew = "";
var isTemplet = "N";
/**
 * 编辑数据
 */
var editIndex = undefined;

function endEditing() {
    if (editIndex == undefined) {
        return true;
    }
    if ($('#cbbTabColSortDataGrid').datagrid('validateRow', editIndex)) {
        $('#cbbTabColSortDataGrid').datagrid('endEdit', editIndex);
        editIndex = undefined;
        return true;
    } else {
        parent.parent.parent.$.messager.show({
        	//commonLang.tip.alert,'提示'
            title: commonLang.tip.alert,//'提示',
            //commonLang.tip.checkData"请检查数据是否正确！"
            msg: commonLang.tip.checkData,
            timeout: 2000,
            showType: 'slide'
        });
        return false;
    }
}
/**
 * 子表追加行
 */
function append() {
    if ($('#cbbTabSortDataGrid').datagrid("getSelected") != null) {
        var tid = $('#cbbTabSortDataGrid').datagrid("getSelected").TABLE_ID; // 主表表ID
        var cid = $('#cbbTabSortDataGrid').datagrid("getSelected").COL_ID; // 主表列ID
        // 验证子表是否已经存在主表的选择字段
        var rows = $('#cbbTabColSortDataGrid').datagrid("getRows");
        for (var i = 0, length = rows.length; i < length; i++) {
            if (rows[i].COLUMN_ID == cid) {
                //commonLang.tip.alert'提示',ommonLang.tip.alert13 '此字段已是排序字段，请勿重复添加！'
            	$.messager.alert(commonLang.tip.alert,commonLang.tip.alert13,'warning');
                return false;
            }
        }
        var cname = $('#cbbTabSortDataGrid').datagrid("getSelected").COL_LABEL; // 主表列中文名
        var num = $('#cbbTabColSortDataGrid').datagrid('getRows').length + 1;
        if (endEditing()) {
            $('#cbbTabColSortDataGrid').datagrid('appendRow', {
                ID: '',
                TABLE_ID: tid,
                COLUMN_ID: cid,
                SORT_ORDER: num,
                COL_LABEL: cname,
                SORT_CODE: "A"
            });
            editIndex = $('#cbbTabColSortDataGrid').datagrid('getRows').length - 1;
            $('#cbbTabColSortDataGrid').datagrid('selectRow', editIndex)
                .datagrid('beginEdit', editIndex);
        }
    } else {
        //commonLang.tip.alert '提示',commonLang.tip.operateAfter'请选中字段后，再进行操作！'
    	$.messager.alert(commonLang.tip.alert,commonLang.tip.operateAfter,'warning');
    }
}
/**
 * 删除所选的行
 */
function removeit() {
    var rows = $('#cbbTabColSortDataGrid').datagrid("getChecked");
    var deleteData = JSON.stringify(rows);
    if (rows.length > 0) {
        $.messager
            .confirm(
                //commonLang.tip.confirm '请确认',commonLang.tip.confirmContent '您确定要删除当前所选的数据？'
            		commonLang.tip.confirm,
            		commonLang.tip.confirmContent,
                function(b) {
                    if (b) {
                        $
                            .ajax({
                                url: 'platform/aos/commtablemanagerController/deleteCbbTabSortData.json',
                                data: {
                                    deleteData: deleteData
                                },
                                type: 'post',
                                dataType: 'json',
                                success: function(result) {
                                    if (result.flag == "success") {
                                        for (var i = 0, length = rows.length; i < length; i++) {
                                            var index = $(
                                                    '#cbbTabColSortDataGrid')
                                                .datagrid(
                                                    'getRowIndex',
                                                    rows[i]);
                                            $(
                                                    '#cbbTabColSortDataGrid')
                                                .datagrid(
                                                    'cancelEdit',
                                                    index)
                                                .datagrid(
                                                    'deleteRow',
                                                    index);
                                        }
                                        editIndex = undefined;
                                        reload();
                                        parent.parent.parent.$.messager.show({
                                            title: commonLang.tip.alert,//'提示',
                                            //commonLang.optip.success参数替换"删除成功！"
                                            msg: commonLang.optip.success
                                        });
                                    } else {
                                    	parent.parent.parent.$.messager.show({
                                            title: commonLang.tip.alert,//'提示',
                                            msg: result.mes
                                        });
                                    }
                                }
                            });
                    }
                });
    } else {
        //commonLang.tip.alert,'提示',commonLang.tip.content2'请选择要删除的记录！'
    	$.messager.alert(commonLang.tip.alert,commonLang.tip.content2,'warning');
    }
}
/**
 * 提交改变的记录
 */
function accept() {
    if (endEditing()) {
        // 新增字段
        var insertRows = $("#cbbTabColSortDataGrid").datagrid('getChanges',
            'inserted');
        var insertData = JSON.stringify(insertRows);
        var updateRows = $("#cbbTabColSortDataGrid").datagrid('getChanges',
            'updated');
        var updateData = JSON.stringify(updateRows);
        $
            .ajax({
                url: 'platform/aos/commtablemanagerController/saveCbbTabColSortData.json',
                data: {
                    insertData: insertData,
                    updateData: updateData,
                    sysId: sysId
                },
                type: 'post',
                dataType: 'json',
                success: function(result) {
                    if (result.flag == "success") {
                        reload();
                        parent.parent.parent.$.messager.show({
                            title: commonLang.tip.alert,//'提示',
                            //commonLang.optip.saveSuccess "保存成功！"
                            msg: commonLang.optip.saveSuccess
                        });
                    } else {
                    	parent.parent.parent.$.messager.show({
                            title: commonLang.tip.alert,//提示,
                            msg: result.mes
                        });
                    }
                }
            });
    }
}
/**
 * 转换排序标识
 * 
 * @param value
 * @param row
 * @param index
 * @returns
 */
function fomaterType1(value, row, index) {
    var type = "";
    if (value == 'A') {
        type = commonLang.fomaterType.asc;//"升序";
    } else if (value == 'D') {
        type = commonLang.fomaterType.desc;//"降序";
    }
    if (row.COL_IS_DISPLAY == "N" && row.COL_IS_SYS == "Y") {
        return "<font color='red'>" + type + "</font>";
    } else {
        return type;
    }
}

function fomaterType2(value, row, index) {
    if (value == 'A') {
        return commonLang.fomaterType.asc;//"升序";
    } else if (value == 'D') {
        return commonLang.fomaterType.desc;//"降序";
    }
}

// 上移
function MoveUp(remind,content) {
    if (endEditing()) {
        var row = $("#cbbTabColSortDataGrid").datagrid('getSelected');
        if (row == null) {
            //commonLang.tip.alert'提示',commonLang.tip.content23'请高亮选择要上移的记录！'
        	$.messager.alert(commonLang.tip.alert,commonLang.tip.content23,'warning');
            return false;
        }
        var index = $("#cbbTabColSortDataGrid").datagrid('getRowIndex', row);
        mysort(index, 'up', 'cbbTabColSortDataGrid');
    }
}
// 下移
function MoveDown(remind,content) {
    if (endEditing()) {
        var row = $("#cbbTabColSortDataGrid").datagrid('getSelected');
        if (row == null) {
            //commonLang.tip.alert'提示',commonLang.tip.content24'请高亮选择要下移的记录！'
        	$.messager.alert(commonLang.tip.alert,commonLang.tip.content24,'warning');
            return false;
        }
        var index = $("#cbbTabColSortDataGrid").datagrid('getRowIndex', row);
        mysort(index, 'down', 'cbbTabColSortDataGrid');
    }
}

// 上移下移的具体方法
function mysort(index, type, gridname) {
    if ("up" == type) {
        if (index != 0) {
            var toup = $('#' + gridname).datagrid('getData').rows[index];
            var upRow = toup.SORT_ORDER; // 选择行排序号
            var todown = $('#' + gridname).datagrid('getData').rows[index - 1];
            var downRow = todown.SORT_ORDER; // 目标行排序号
            $('#' + gridname).datagrid('selectRow', index).datagrid('endEdit',
                index);
            $('#' + gridname).datagrid('getData').rows[index] = todown;
            $('#' + gridname).datagrid('getData').rows[index - 1] = toup;

            $('#' + gridname).datagrid('selectRow', index).datagrid(
                'beginEdit', index);
            var uped = $('#' + gridname).datagrid('getEditor', {
                index: index,
                field: 'SORT_ORDER'
            });
            $(uped.target).numberbox('setValue', upRow);
            $('#' + gridname).datagrid('selectRow', index - 1).datagrid(
                'beginEdit', index - 1);
            var doed = $('#' + gridname).datagrid('getEditor', {
                index: (index - 1),
                field: 'SORT_ORDER'
            });
            $(doed.target).numberbox('setValue', downRow);

            $('#' + gridname).datagrid('selectRow', index - 1).datagrid(
                'endEdit', index - 1);
            $('#' + gridname).datagrid('selectRow', index).datagrid('endEdit',
                index);
            $('#' + gridname).datagrid('selectRow', index - 1);
            $('#' + gridname).datagrid('uncheckRow', index);
        }
    } else if ("down" == type) {
        var rows = $('#' + gridname).datagrid('getRows').length;
        if (index != rows - 1) {
            var todown = $('#' + gridname).datagrid('getData').rows[index];
            var downRow = todown.SORT_ORDER; // 选择行排序号
            var toup = $('#' + gridname).datagrid('getData').rows[index + 1];
            var upRow = toup.SORT_ORDER; // 目标行排序号
            $('#' + gridname).datagrid('selectRow', index).datagrid('endEdit',
                index);
            $('#' + gridname).datagrid('getData').rows[index + 1] = todown;
            $('#' + gridname).datagrid('getData').rows[index] = toup;

            $('#' + gridname).datagrid('selectRow', index + 1).datagrid(
                'beginEdit', index + 1);
            var uped = $('#' + gridname).datagrid('getEditor', {
                index: (index + 1),
                field: 'SORT_ORDER'
            });
            $(uped.target).numberbox('setValue', upRow);
            $('#' + gridname).datagrid('selectRow', index).datagrid(
                'beginEdit', index);
            var doed = $('#' + gridname).datagrid('getEditor', {
                index: index,
                field: 'SORT_ORDER'
            });
            $(doed.target).numberbox('setValue', downRow);

            $('#' + gridname).datagrid('selectRow', index + 1).datagrid(
                'endEdit', index + 1);
            $('#' + gridname).datagrid('selectRow', index).datagrid('endEdit',
                index);
            $('#' + gridname).datagrid('selectRow', index + 1);
            $('#' + gridname).datagrid('uncheckRow', index);
        }
    }
}

/**
 * 子表点击行事件
 */
function onClickRowCode(rowIndex, rowData) {
    // 设置编辑行
    if (editIndex != rowIndex) {
        if (endEditing()) {
            $('#cbbTabColSortDataGrid').datagrid('selectRow', rowIndex)
                .datagrid('beginEdit', rowIndex);
            editIndex = rowIndex;
        } else {
            $('#cbbTabColSortDataGrid').datagrid('selectRow', editIndex);
        }
    }
}

/**
 * 保存后刷新表格
 */
function reload() {
    var url1 = 'platform/aos/commtablemanagerController/getCbbTabSortData.json?sysId=' + sysId + '&tableName=' + tableName + '&isTemplet=' + isTemplet;
    $('#cbbTabSortDataGrid').datagrid("options").url = url1;
    $("#cbbTabSortDataGrid").datagrid('load', {
        param: null
    });

    var url2 = 'platform/aos/commtablemanagerController/getCbbTabColSortData.json?sysId=' + sysId + '&tableName=' + tableName;
    $('#cbbTabColSortDataGrid').datagrid("options").url = url2;
    $("#cbbTabColSortDataGrid").datagrid('load', {
        param: null
    });
}

function selectAllCol() {
    if (isTemplet == 'Y') { // 显示基本字段
        isTemplet = 'N';
        document.getElementById("selectAllCol").style.display = '';
        document.getElementById("selectCol").style.display = 'none';

    } else { // 显示所有字段
        isTemplet = 'Y';
        document.getElementById("selectAllCol").style.display = 'none';
        document.getElementById("selectCol").style.display = '';

    }
    var url = 'platform/aos/commtablemanagerController/getCbbTabSortData.json?sysId=' + sysId + '&tableName=' + tableName + '&isTemplet=' + isTemplet;
    $('#cbbTabSortDataGrid').datagrid("options").url = url;
    $("#cbbTabSortDataGrid").datagrid('load', {
        param: null
    });
}

function fomaterColOrder(value, row, index) {
    if (value == null || value == "null") {
        value = "";
    }
    if (row.COL_IS_DISPLAY == "N" && row.COL_IS_SYS == "Y") {
        return "<font color='red'>" + value + "</font>";
    } else {
        return value;
    }
}

function fomaterColLabel(value, row, index) {
    if (value == null || value == "null") {
        value = "";
    }
    if (row.COL_IS_DISPLAY == "N" && row.COL_IS_SYS == "Y") {
        return "<font color='red'>" + value + "</font>";
    } else {
        return value;
    }
}

function fomaterSortOrder(value, row, index) {
    if (value == null || value == "null") {
        value = "";
    }
    if (row.COL_IS_DISPLAY == "N" && row.COL_IS_SYS == "Y") {
        return "<font color='red'>" + value + "</font>";
    } else {
        return value;
    }
}